package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;

import br.indt.framework.utils.StringUtils;

public class TextFieldFullTouch extends TextField implements TextBoxUpdateListener {
	
	public TextFieldFullTouch(String name, String title, String text, int maxSize, int constraints, int minHeight) {
		super(name, title, text, maxSize, constraints, minHeight);
		
	}
	
	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight) {
		int tmpWidth = width - (leftMargin + rightMargin);
		x += leftMargin;
		y += topMargin;
		
		g.setClip(x, y, maxWidth, height);
		
		if(!labelHeading.getText().equals(StringUtils.EMPTY)){
			labelHeading.paint(g, x, y, maxWidth, maxHeight);
			y += labelHeading.getHeight(); 
		}

		g.setClip(x, y, maxWidth, height);
		
		setBackgroundTextFieldOnSelection(g, x, y, pressedDown);
		
		verifyMarkupText();
		
		labelTxt.paint(g, x, y, tmpWidth, maxHeight);
		
	}
	
	
	public void click(int x, int y) {
		try {
			clearMarkupText();
		} catch (Exception e) {
			System.out.println("[ERROR]TextFieldFullTouch::Click " + e);
		}
	}
	
	public void textUpdated(String txt) {
		String aux = labelTxt.getText();
		
		labelTxt.setText(txt);
		
		if (this.textFieldUpdateListener != null){
			this.textFieldUpdateListener.textFieldUpdated(txt,aux);
		}
		
		if (this.textFieldListener != null){
			this.textFieldListener.textUpdated(txt);
		}
		
		calculateHeight();
		try {
			Display.getInstance().refresh(true);
		} catch (Exception e) {
//			Utils.log("[ERROR]TextFieldFullTouch::textUpdated " + e);
		}
	}

	public void pressFire() {
		click(0, 0);
	}
	
}
